<template>
  <div class="register-container">
    <el-card header="欢迎注册" class="register-card">
      <el-form @submit.native.prevent="register">
        <el-form-item label="用户名">
          <el-input v-model="model.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="model.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" native-type="submit">提交</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-button type="text" @click="toLogin">已有账号？立即登录</el-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      model: {}
    };
  },
  methods: {
    async register() {
      const res = await this.$http.post("user/register", this.model);
      if (res) {
        this.$router.push("/login");
        this.$message({
          type: "success",
          message: "注册成功"
        });
      }
    },
    toLogin(){
        this.$router.push("/login")
    }
  }
};
</script>

<style>
.register-card {
  width: 25rem;
  margin: 5rem auto;
}
</style>